﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.goods
{
    public partial class color_list : SF.Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        public string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.pageSize = GetPageSize(10); //每页数量
            this.keywords = DTRequest.GetQueryString("keywords");
            if (!Page.IsPostBack)
            {
                Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                string busnieseNum = mgr.busnieseNum.ToString();
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
                string templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();
                RptBind("id>0 and " + Utils.getWhereByInfo(busnieseNum, appNum, templatesNum) + CombSqlTxt(this.keywords));
            }
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;
            BLL.sf_goods_color colorBll = new BLL.sf_goods_color();
            this.rptList.DataSource = colorBll.GetList(this.pageSize, this.page, _strWhere, "id asc", out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("color_list.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and colorName like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion


        #region 返回图文每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("goods_color_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("color_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("goods_color_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("color_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int sucCount = 0;
            int errorCount = 0;
            BLL.sf_goods_color colorBll = new BLL.sf_goods_color();
            //批量删除
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                long id = Convert.ToInt64(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (colorBll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除颜色信息成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("color_list.aspx", "keywords={0}", txtKeywords.Text.Trim()));
        }

        public string getTypeNameByNum(string num)
        {
            BLL.sf_goods_alltype alltypeBll = new BLL.sf_goods_alltype();
            return alltypeBll.GetModelByNum(num).typeName;
        }
    }
}